package com.tongfusoft.document.web.controller.administration;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.RoleHasSecurities;
import com.tongfusoft.document.entity.RoleHasUsers;
import com.tongfusoft.document.entity.Roles;
import com.tongfusoft.document.entity.SecurityGroup;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.role.service.IDocumentRoleService;
import com.tongfusoft.document.manager.security.service.IDocumentSecurityGroupService;
import com.tongfusoft.document.manager.user.service.IDocumentUserService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.DataTable;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.UIDGeneratorService;

@RequestMapping("/manager")
@Controller
public class RoleController extends BaseController {

	@Autowired
	@Qualifier(value = "DocumentRoleService")
	public IDocumentRoleService roleService;

	@UrlDesc(value = "角色管理页")
	@RequestMapping("/role")
	public String role(HttpServletRequest request, Model model) {
		model.addAttribute("mainPageKey", "role");
		model.addAttribute("mainPage", "manager/role/role");
		return "base";
	}

	@UrlDesc(value = "角色信息列表(分页)")
	@RequestMapping("/role/query")
	@ResponseBody
	public DataTable<Roles> roleQuery(Model model, int length, int start,
			int draw) {
		DataTable rm = new DataTable();
		try {
			Roles role = new Roles();
			int pageIndex = (start / length);
			PageInfo pageInfo = this.roleService.rolePageTable(pageIndex,
					length, role);
			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rm;
	}
	
	@UrlDesc(value = "角色信息列表")
	@RequestMapping("/roleList/query")
	@ResponseBody
	public ResultModel roleListQuery(HttpServletRequest request, Users user,
			Model model) {
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			List<Roles> list = this.roleService.roleListById(user);
			rm.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rm;
	}
	
	@UrlDesc(value = "添加角色")
	@RequestMapping("/role/add")
	@ResponseBody
	public ResultModel roleAdd(Roles roles, RoleHasSecurities roleHasSecurities) {
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			this.roleService.createRole(roles, (Users) super.getUser());
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;
	}

	@UrlDesc(value = "删除角色")
	@RequestMapping("/role/del")
	@ResponseBody
	public ResultModel roleDel(Roles roles, RoleHasSecurities roleHasSecurities) {
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			this.roleService.delRole(roles, (Users) super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "修改角色")
	@RequestMapping("/role/edit")
	@ResponseBody
	public ResultModel roleEdit(Roles roles) {
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			this.roleService.updateRole(roles, (Users) super.getUser());
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "角色详情")
	@RequestMapping("/role/detail")
	@ResponseBody
	public ResultModel roleDetail(Roles roles) {
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			rm.setData(this.roleService.getRole(roles, (Users) super.getUser()));
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "用户角色权限绑定与否")
	@RequestMapping("/isRoleHasUsers")
	@ResponseBody
	public ResultModel roleHasUsersGet(RoleHasUsers roleHasUsers, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			List<RoleHasUsers> list = this.roleService.getRoleHasUser(roleHasUsers);
			rm.setData(list);
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "对应角色关联的用户")
	@RequestMapping("/userList/query")
	@ResponseBody
	public DataTable<RoleHasUsers> userQuery(Model model, int length,
			int start, int draw, String roleId, String order) {
		DataTable rm = new DataTable();
		try {
			RoleHasUsers roleHasUsers = new RoleHasUsers();
			roleHasUsers.setRoleId(roleId);
			int pageIndex = (start / length);
			PageInfo pageInfo = this.roleService.getUserHasRole(pageIndex,
					length, roleHasUsers);

			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rm;
	}

	@UrlDesc(value = "对应角色未关联用户")
	@RequestMapping("/userUnSelect/query")
	@ResponseBody
	public ResultModel userListUnSelect(HttpServletRequest request, Roles role,
			Model model) {
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			List<Users> list = this.roleService.userListById(role);
			rm.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rm;
	}

	@UrlDesc(value = "添加用户角色关联")
	@RequestMapping("/userHasRoles/add")
	@ResponseBody
	public ResultModel userHasRolesAdd(HttpServletRequest request, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		RoleHasUsers ru = new RoleHasUsers();
		try {
			String roleId = request.getParameter("roleId");
			String userIdList = request.getParameter("userIdList");
			String[] userList = userIdList.split(",");
			this.roleService.saveUsersHasRole(userList,roleId,(Users) super.getUser());
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	
	
	//以下为角色安全组关联权限操作
	@UrlDesc(value = "角色安全组权限列表分页")
	@RequestMapping("/powerList/query")
	@ResponseBody
	public DataTable<RoleHasSecurities> powerQuery(Model model, int length,
			int start, int draw, String roleId, String order) {
		DataTable rm = new DataTable();
		try {
			RoleHasSecurities roleHasSecurities = new RoleHasSecurities();
			roleHasSecurities.setRoleId(roleId);
			int pageIndex = (start / length);
			PageInfo pageInfo = this.roleService.roleHasSecuritiesPageTable(
					pageIndex, length, roleHasSecurities);

			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rm;
	}

	@UrlDesc(value = "权限详情")
	@RequestMapping("/power/detail")
	@ResponseBody
	public ResultModel powerDetail(RoleHasSecurities roleHasSecurities,
			String rpId) {
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			rm.setData(this.roleService.powerByRpId(rpId));
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "编辑权限")
	@RequestMapping("/power/edit")
	@ResponseBody
	public ResultModel powerEdit(RoleHasSecurities roleHasSecurities) {
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			this.roleService.updateRoleHasGroup(roleHasSecurities,
					(Users) super.getUser());
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}
}
